{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "S = \"(a(b(c)d)\"\n",
    "\n",
    "def theFunction(match):\n",
    "    print(match.group(\"target\"))\n",
    "    print(match.group(\"another_one\"))\n",
    "    return \"\"\n",
    "\n",
    "S = re.sub(r\"\\((?P<target>[^()]*?)\\)(?P<another_one>\\w)\", theFunction,S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "inputs = \"(a(b(c)d)\"\n",
    "\n",
    "S = inputs\n",
    "while 1:\n",
    "    temp = S\n",
    "    S = re.sub(r\"\\((?P<expression>[^()]*?)\\)\", r\"[\\g<expression>]\",S)\n",
    "    if temp == S:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'(a[b[c]d]'"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'(a(b(c)d)'"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "'return' outside function (<ipython-input-84-f554e9d02efa>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-84-f554e9d02efa>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m    return \"\"\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m 'return' outside function\n"
     ]
    }
   ],
   "source": [
    "if inputs == S:\n",
    "    if \"(\" in S or \")\" in S:\n",
    "        return \"\"\n",
    "    return inputs\n",
    "else:\n",
    "    return S.replace(\"(\", \"\").replace(\")\", \"\").replace(\"[\", \"(\").replace(\"]\", \")\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def minRemoveToMakeValid(self, s: str) -> str:\n",
    "        inputs = s\n",
    "        \n",
    "        S = inputs\n",
    "        while 1:\n",
    "            temp = S\n",
    "            S = re.sub(r\"\\((?P<expression>[^()]*?)\\)\", r\"[\\g<expression>]\",S)\n",
    "            if temp == S:\n",
    "                break\n",
    "        \n",
    "        print(\"after re :\", S)\n",
    "        \n",
    "        if inputs == S:\n",
    "            if \"(\" in S or \")\" in S:\n",
    "                return \"\"\n",
    "            return inputs\n",
    "        else:\n",
    "            return S.replace(\"(\", \"\").replace(\")\", \"\").replace(\"[\", \"(\").replace(\"]\", \")\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after re : (a[b[[c]]d]\n",
      "a(b((c))d)\n",
      "after re : lee[t[c]o]de)\n",
      "lee(t(c)o)de\n",
      "after re : [])[](((\n",
      "()()\n"
     ]
    }
   ],
   "source": [
    "print(Solution().minRemoveToMakeValid(\"(a(b((c))d)\"))\n",
    "print(Solution().minRemoveToMakeValid(\"lee(t(c)o)de)\"))\n",
    "print(Solution().minRemoveToMakeValid(\"())()(((\"))\n",
    "#\"()()\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/\n",
    "\n",
    "\n",
    "Runtime: 1280 ms, faster than 10.55% of Python3 online submissions for Minimum Remove to Make Valid Parentheses.\n",
    "\n",
    "Memory Usage: 17.4 MB, less than 5.44% of Python3 online submissions for Minimum Remove to Make Valid Parentheses.\n",
    "\n",
    "\n",
    "```python\n",
    "class Solution:\n",
    "    def minRemoveToMakeValid(self, s: str) -> str:\n",
    "        inputs = s\n",
    "        \n",
    "        S = inputs\n",
    "        while 1:\n",
    "            temp = S\n",
    "            S = re.sub(r\"\\((?P<expression>[^()]*?)\\)\", r\"[\\g<expression>]\",S)\n",
    "            if temp == S:\n",
    "                break\n",
    "        \n",
    "        print(\"after re :\", S)\n",
    "        \n",
    "        if inputs == S:\n",
    "            if \"(\" in S or \")\" in S:\n",
    "                return \"\"\n",
    "            return inputs\n",
    "        else:\n",
    "            return S.replace(\"(\", \"\").replace(\")\", \"\").replace(\"[\", \"(\").replace(\"]\", \")\")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
